package com.atguigu.gulimall.product.service.impl;

import com.atguigu.common.utils.Query;
import jdk.nashorn.internal.ir.CallNode;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;

import com.atguigu.gulimall.product.dao.AttrGroupDao;
import com.atguigu.gulimall.product.entity.AttrGroupEntity;
import com.atguigu.gulimall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        // 先将key放入查询条件
        Object key = params.get("key");
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<AttrGroupEntity>();
        if(key != null && !StringUtils.isEmpty(key.toString()))
        {
            queryWrapper.and(obj -> {
                obj.like("catelog_id", key).or().like("attr_group_name", key);
            });
        }

        // 没有指定具体的分类就查所有分类，同时带key
        if(catelogId == 0){
            // 使用自己封装的方法，将请求params封装为IPage对象
            IPage<AttrGroupEntity> pageParams = new Query<AttrGroupEntity>().getPage(params);
            IPage<AttrGroupEntity> page = this.page(pageParams, queryWrapper);
            return new PageUtils(page);
        }else {
                queryWrapper.and(obj->obj.eq("catelog_id", catelogId));
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), queryWrapper);
            return new PageUtils(page);
        }
    }

}
